<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Event Model
 * @description : Database functions related to calander events
 * 
 */
class Event_model extends CI_Model {

  function __construct() {
    parent::__construct();
  }

  // Create a new user event
  function set_event($event_date = NULL, $event_msg = NULL, $event_user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($event_date) && !empty($event_msg) && !empty($event_user_id) && is_numeric($event_user_id)) {
      $query_insr = array(
          'event_message' => $event_msg,
          'event_user_id' => $event_user_id,
          'event_date' => date("Y-m-d H:i:s", strtotime($event_date)),
          'event_status' => 1
      );
      $query_exec = $this->db->insert('events', $query_insr);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result']['event_id'] = $this->db->insert_id();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  // Get birthday events of a date
  function get_birthdays($date_data = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($date_data)) {
      $query_cond = array();
      $query_cond['user_status'] = 1;
      $query_cond['user_is_user'] = 1;
      $query_cond['user_resigned'] = 0;
      $query_cond['DATE_FORMAT(user_birthday_date,"%d%m")'] = date('dm', strtotime($date_data));
      $query_exec = $this->db->get_where('users', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Get joines of a date
  function get_joindays($date_data = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($date_data)) {
      $query_cond = array();
      $query_cond['user_status'] = 1;
      $query_cond['user_is_user'] = 1;
      $query_cond['user_resigned'] = 0;
      $query_cond['DATE_FORMAT(user_join_date,"%d%m")'] = date('dm', strtotime($date_data));
      $query_exec = $this->db->get_where('users', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Get user event of a date
  function get_user_event($date_data = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($date_data)) {
      $query_cond = array();
      $query_cond['user_status'] = 1;
      $query_cond['user_is_user'] = 1;
      $query_cond['event_status'] = 1;
      $query_cond['user_resigned'] = 0;
      $query_cond['DATE_FORMAT(event_date,"%Y%d%m")'] = date('Ydm', strtotime($date_data));
      $this->db->join('users', 'user_id = event_user_id');
      $this->db->order_by('event_date', 'desc');
      $query_exec = $this->db->get_where('events', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Get all birthdays
  function get_all_birthdays() {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    $query_cond['user_status'] = 1;
    $query_cond['user_is_user'] = 1;
    $query_cond['user_resigned'] = 0;
    $query_exec = $this->db->get_where('users', $query_cond);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->result_array();
      $result_data['count'] = $query_exec->num_rows();
    }
    return $result_data;
  }

  // Get all join dates
  function get_all_joindays() {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    $query_cond['user_status'] = 1;
    $query_cond['user_is_user'] = 1;
    $query_cond['user_resigned'] = 0;
    $query_exec = $this->db->get_where('users', $query_cond);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->result_array();
      $result_data['count'] = $query_exec->num_rows();
    }
    return $result_data;
  }

  function get_all_user_events() {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    $query_cond['user_status'] = 1;
    $query_cond['user_is_user'] = 1;
    $query_cond['user_resigned'] = 0;
    $query_cond['event_status'] = 1;
    $query_cond['event_date >='] = date('Y-m-d H:i:s');
    $this->db->join('users', 'user_id = event_user_id');
    $this->db->order_by('event_date', 'desc');
    $query_exec = $this->db->get_where('events', $query_cond);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->result_array();
      $result_data['count'] = $query_exec->num_rows();
    }
    return $result_data;
  }

  /*
   * Events of this month
   */

  function get_this_birthdays() {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    $query_cond['user_status'] = 1;
    $query_cond['user_is_user'] = 1;
    $query_cond['user_resigned'] = 0;
    $query_cond['DATE_FORMAT(user_birthday_date,"%m%d") >='] = date('md');
    //$query_cond['DATE_FORMAT(user_birthday_date,"%m")'] = date('m');
    $query_exec = $this->db->get_where('users', $query_cond);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->result_array();
      $result_data['count'] = $query_exec->num_rows();
    }
    return $result_data;
  }

  function get_this_joindays() {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    $query_cond['user_status'] = 1;
    $query_cond['user_is_user'] = 1;
    $query_cond['user_resigned'] = 0;
    $query_cond['DATE_FORMAT(user_join_date,"%m%d") >='] = date('md');
    //$query_cond['DATE_FORMAT(user_join_date,"%m")'] = date('m');
    $query_exec = $this->db->get_where('users', $query_cond);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->result_array();
      $result_data['count'] = $query_exec->num_rows();
    }
    return $result_data;
  }

  function get_this_user_events() {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    $query_cond['user_status'] = 1;
    $query_cond['user_is_user'] = 1;
    $query_cond['user_resigned'] = 0;
    $query_cond['event_status'] = 1;
    $query_cond['event_date >='] = date('Y-m-d');
    //$query_cond['DATE_FORMAT(event_date,"%m")'] = date('m');
    $this->db->join('users', 'user_id = event_user_id');
    $this->db->order_by('event_date', 'desc');
    $query_exec = $this->db->get_where('events', $query_cond);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->result_array();
      $result_data['count'] = $query_exec->num_rows();
    }
    return $result_data;
  }

  function get_my_events($user_id = NULL, $limit = 10) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($user_id) && is_numeric($user_id)) {
      $query_cond = array();
      $query_cond['event_user_id'] = $user_id;
      $query_cond['event_status'] = 1;
      $this->db->order_by('event_id', 'desc');
      $query_exec = $this->db->get_where('events', $query_cond, $limit);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_event_by_id($event_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($event_id) && is_numeric($event_id)) {
      $query_cond = array();
      $query_cond['event_id'] = $event_id;
      $query_cond['event_status'] = 1;
      $this->db->join('users', 'user_id = event_user_id');
      $query_exec = $this->db->get_where('events', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function update_event_by_id($event_id = NULL, $event_date = NULL, $event_msg = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($event_id) && is_numeric($event_id) && !empty($event_date) && !empty($event_msg)) {
      $query_upda = array();
      $query_upda['event_date'] = date("Y-m-d H:i:s", strtotime($event_date));
      $query_upda['event_message'] = $event_msg;
      $query_cond = array();
      $query_cond['event_id'] = $event_id;
      $this->db->order_by('event_id', 'desc');
      $query_exec = $this->db->update('events', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function delete_event_by_id($event_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($event_id)) {
      $query_upda = array();
      $query_upda['event_status'] = 0;
      $query_cond = array();
      $query_cond['event_id'] = $event_id;
      $this->db->order_by('event_id', 'desc');
      $query_exec = $this->db->update('events', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function upd_event_notify_status($event_id = NULL, $notify_status = 1) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($event_id) && is_numeric($event_id)) {
      $query_cond = array();
      $query_cond['event_id'] = $event_id;
      $query_upda = array();
      $query_upda['event_notification'] = $notify_status;
      $query_exec = $this->db->update('events', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

}

?>
